package com.wuxin.ssm.controller;


import com.wuxin.ssm.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @program: untitled
 * @ClassName AdminController
 * @description: 后台登录
 * @author: 李尧坤
 * @create: 2024−12-02 17:01
 * @Version 1.0
 **/

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    //http://localhost:8080/ssmxiangmu_war_exploded/admin/login.action

    /**
     *  后台登录功能
     * @param name
     * @param pwd
     * @param model
     * @return
     */

     /*   @RequestMapping("/login.action")
    public String login(String name, String pwd, HttpSession session){
        //登录方法
      boolean flag= adminService.doLogin(name,pwd);

      if(flag){
           //登录成功  了  存用户信息
          session.setAttribute("adminName",name);
          return "redirect:/admin/main.jsp";
      }


        return "redirect:/login.jsp";
    }*/





    @RequestMapping("/login.action")
    public String login(String  name, String pwd, Model model){

        //走视图解析器
        String str = adminService.login(name,pwd);
        if ("ok".equals(str)){
            //合法
            model.addAttribute("adminName",name);
            return "main";
        } else {
            model.addAttribute("errmsg","用户名或密码错误！");
            return "login";
        }
    }

    @RequestMapping("/logout.action")
    public String logout(Model model) {
        // 清除 Model 中的登录信息
        model.asMap().clear();
        // 重定向到登录页面
        return "redirect:login.jsp";
    }
}
